package com.dianqiu.common.job;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.dianqiu.base.action.BaseAction;
import com.dianqiu.base.action.BaseForJopAction;
import com.dianqiu.common.bean.DqBetUserMatchHistTO;
import com.dianqiu.common.bean.DqHandicapTO;
import com.dianqiu.common.bean.DqMatchHistTO;
import com.dianqiu.common.bean.DqMatchPoolTO;
import com.dianqiu.common.bean.DqMatchTO;
import com.dianqiu.common.bean.DqUserBetConfigTO;

public class Job1 extends BaseForJopAction
{
	private static Logger logger = LoggerFactory.getLogger(Job1.class);

	public void execute() {
		List userBetList = this.getUserBetConfigService().getAllUserBetConfig();
		List matchList = this.getBetUserMatchHistService().getAllDqBetUserMatchHist();
		for(int i = 0 ; i < matchList.size() ; i++)
		{
			DqBetUserMatchHistTO matchHist = (DqBetUserMatchHistTO) matchList.get(i);
			this.getMoney(matchHist,matchHist.getDqMatchHist().getDqCodeTblByWeathCd().getCdValue());
			
		}
	
		
	}
	public void getMoney(DqBetUserMatchHistTO matchHist,String type)
	{
		
		String userId = matchHist.getId().getUserId();
		DqUserBetConfigTO userBetConfigTO = this.getUserBetConfigService().getUserBetConfigByUserId(userId);
		System.out.println(userBetConfigTO+"====");
		
		double profit = userBetConfigTO.getProfit();
		
		int companyId = userBetConfigTO.getDqBetCompany().getCmpyId();
		int matchId = matchHist.getDqMatchHist().getMthId();
		DqHandicapTO handIcap = this.getHandicapService().getHandicapByBetCompanyIdAndMatchId(companyId, matchId);
		double pankou = handIcap.getHomeClbRate()/handIcap.getAwayClbRate();
		double pay = matchHist.getStake();
		double money = 0 ;
		if("-0.5".equals(type))
		{
			if(pankou!=1)
			{
				money = (profit*2+pay/2)/(pankou-1);
			}
		}
		if("0".equals(type))
		{
			if(pankou!=1)
			{
				money = (profit*2)/(pankou-1);
			}
		}
		if("0.5".equals(type))
		{
			if(pankou!=1)
			{
				money = profit/(pankou-1);
			}
		}
		if("-1".equals(type))
		{
			if(pankou!=1)
			{
				money = (profit*2+pay)/(pankou-1);
			}
		}
		DqMatchPoolTO matchPool = new DqMatchPoolTO();
		matchPool.setDqCodeTbl(matchHist.getDqMatchHist().getDqCodeTblByWeathCd())		;
		DqMatchTO matchTO = (DqMatchTO) this.getMatchService().getDqMatchTOById(matchId).get(0); 
		matchPool.setDqMatch(matchTO);
		matchPool.setWinPercnetage(money);
		matchPool.setBettedInd(1);
		matchPool.setPoolId(1);
		matchPool.setRuleId(0);
		matchPool.setInd("y");
		//String loginUserId = (String) this.getRequest().getSession().getAttribute("userId");
		matchPool.setUserId("eric");
		this.getMatchPoolService().save(matchPool);
	}

}
